package com.qeegoo.b2bautozityp.moudle.login.view;

import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.databinding.ActivityLoginBinding;
import com.qeegoo.b2bautozityp.injector.component.DaggerLoginComponent;
import com.qeegoo.b2bautozityp.moudle.base.BaseActivity;
import com.qeegoo.b2bautozityp.moudle.login.viewmodel.LoginViewModel;

import javax.inject.Inject;

/**
 * Created by lining on 2017/9/15.
 */

public class LoginActivity extends BaseActivity<ActivityLoginBinding> implements OnClickListener {

    @Inject
    LoginViewModel mViewModel;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_login;
    }

    @Override
    protected void initInjector() {
        DaggerLoginComponent.builder().appComponent(getAppComponent()).build().inject(this);
    }

    @Override
    protected void initViews() {
        mBinding.setViewModel(mViewModel);
        mBinding.imgLoginPwdRight.setOnClickListener(this);
        mBinding.editLoginName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                Logger.e("beforeTextChanged："+s.toString());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                Logger.e("onTextChanged："+s.toString());
                mBinding.imgLoginPhoneRight.setImageResource(s.length()>0 ? R.drawable.ic_edit_clear : R.drawable.ic_edit_clear_default);
            }

            @Override
            public void afterTextChanged(Editable s) {
//                Logger.e("afterTextChanged："+s.toString());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_login_pwd_right:{
                if (mViewModel.check.get()){//点击后如果未选中,更改为选中
                    //否则隐藏密码
                    mBinding.editLoginPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mBinding.editLoginPwd.setSelection(mBinding.editLoginPwd.getText().length());
                    mViewModel.check.set(false);
                }else {
                    //如果选中，显示密码
                    mBinding.editLoginPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mBinding.editLoginPwd.setSelection(mBinding.editLoginPwd.getText().length());
                    mViewModel.check.set(true);
                }
                break;
            }
            default:
                break;
        }
    }
}
